package com.ruoyi.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

;

/**
 * @ClassName SendSms
 * @Description
 * @Author huangshun
 * @Date 2021/2/4 0004 下午 2:55
 */
public class SendSms {

    public static ConcurrentHashMap<String, Object> phoneSend(List<String> phoneNumber,List<String> templateParam,String templateID){
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();

        try{
            Credential cred = new Credential(WebConstants.ACCESSKEY, WebConstants.SECRETKEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(WebConstants.Endpoint);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            
            String[] strings = new String[phoneNumber.size()];
            String[] phoneNumberSet1 = phoneNumber.toArray(strings);
            req.setPhoneNumberSet(phoneNumberSet1);
            
            String[] strings2 = new String[templateParam.size()];
            String[] templateParamSet1 = templateParam.toArray(strings2);
            req.setTemplateParamSet(templateParamSet1);
            
            req.setTemplateID(templateID);
            req.setSmsSdkAppid(WebConstants.SmsSdkAppid);
            req.setSign(WebConstants.Sign);

            SendSmsResponse resp = client.SendSms(req);
            JSONObject parse = JSONUtil.parseObj(resp);

            JSONArray jsonArray = JSONUtil.parseArray(parse.get("SendStatusSet"));
            JSONObject jsonObject = JSONUtil.parseObj(jsonArray.get(0));
            if(jsonObject.get("Code").equals("Ok")){
                map.put("code","20000");
                map.put("message","发送成功");
            }else{
                map.put("code","10000");
                map.put("message","短信发送失败");
            }
        } catch (TencentCloudSDKException e) {
            map.put("code","10000");
            map.put("message","短信发送失败");
        }
        return map;
    }

    public static void main(String[] args) {
        List<String> phoneNumber = new ArrayList<>();
        phoneNumber.add("+8618201302597");
        List<String> templateParam= new ArrayList<>();
        // 随机
        templateParam.add(WebConstants.generateNumber());
        //选择模板
        String templateID = "";
        templateID = WebConstants.TEMPLATEID_RESTPASSWORD;
//        ConcurrentHashMap<String, Object> stringObjectConcurrentHashMap = SendSms.phoneSend(phoneNumber, templateParam, templateID);
//        System.out.println(stringObjectConcurrentHashMap);
        long nowTime = System.currentTimeMillis();

        try {
            Thread.sleep(5000);
            long value = System.currentTimeMillis();
            System.out.println(value-nowTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }



//        SendSms.phoneSend(phoneNumber,templateParam,WebConstants.TEMPLATEID_REGISTER);
    }
}
